package com.macro.mall.controller;

import com.google.common.collect.Maps;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.TenantUserDto;
import com.macro.mall.model.TenantEntity;
import com.macro.mall.model.TenantUserEntity;
import com.macro.mall.service.TenantService;
import com.macro.mall.service.TenantUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *  商家用户controller
 */
@RestController
@Api(tags = "TenantUserController", description = "商家用户管理后台")
@RequestMapping("/tenantUser")
public class TenantUserController {

    @Autowired
    private TenantUserService tenantUserService;

    @Autowired
    private TenantService tenantService;

    @ApiOperation(value = "查询商家用户信息info")
    @RequestMapping(value = "/findTenanUserList", method = RequestMethod.POST)
    public CommonResult findTenanUserList(HttpServletRequest request, @RequestBody TenantUserDto tenantUserDto){

        String userName = String.valueOf(request.getAttribute("useName"));
        TenantEntity tenantInfo = tenantService.findTenantInfo(userName);
        Long tenantId = tenantInfo.getId();
        int tenanUserCount = tenantUserService.findTenanUserCount(tenantId);
        Integer curPage = tenantUserDto.getPageNo();
        Integer pageSize = tenantUserDto.getPageSize();
        List<TenantUserEntity> tenanUserList = null;
        if(tenanUserCount > 0){
            tenanUserList = tenantUserService.findTenanUserList(tenantId, curPage, pageSize);
        }
        Map<Object, Object> map = Maps.newHashMap();
        map.put("list", tenanUserList);
        map.put("pageNum", curPage);
        map.put("pageSize", pageSize);
        map.put("total", tenanUserCount);
        Integer totalPage = (tenanUserCount+1)/pageSize + 1 ;
        map.put("totalPage", totalPage);
        return CommonResult.success(map);
    }

}
